package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceType;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.CmdbDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_DEVICE_TYPE;

@RestController
@Api(value = "设备类型管理", tags = {"设备类型管理"})
public class CmdbDeviceTypeController {

    @Autowired
    private CmdbDeviceTypeService cmdbDeviceTypeService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询设备类型
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/deviceType/search")
    @ApiOperation(value = "查询设备类型", notes = "查询设备类型")
    public PlatformResponse<Object> searchDeviceType(@RequestBody Object params) {
        Object result = cmdbDeviceTypeService.search(CMDB_DEVICE_TYPE, params);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(value = "/deviceType/findAll")
    @ApiOperation(value = "查询全部设备类型", notes = "查询全部设备类型")
    public PlatformResponse<Object> findAll() {
        Object result = cmdbDeviceTypeService.findAll();
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceType/addOrUpdate")
    @ApiOperation(value = "修改设备类型", notes = "修改设备类型")
    public PlatformResponse<Void> addOrUpdateDeviceType(@RequestBody CmdbDeviceType cmdbDeviceType) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceTypeService.addOrUpdate(cmdbDeviceType, account);
        return PlatformResponse.successMsg("处理成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceType/delete")
    @ApiOperation(value = "删除设备类型", notes = "删除设备类型")
    public PlatformResponse<Void> deleteDeviceType(@RequestBody CmdbDeviceType cmdbDeviceType) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceTypeService.delete(cmdbDeviceType, account);
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deviceType/deleteList")
    @ApiOperation(value = "批量删除设备类型", notes = "批量删除设备类型")
    public PlatformResponse<Void> deleteListDeviceType(@RequestBody List<String> codes) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDeviceTypeService.deleteByCodeList(codes, account);
        return PlatformResponse.successMsg("删除成功");
    }
}
